// 1. 导入axios
import axios from "axios";

const baseURL = import.meta.env.VITE_BASE_URL;

// 2. 创建实例
const request = axios.create({
    baseURL: baseURL
});

// 携带cookie信息
request.defaults.withCredentials = true;

// 3. 配置响应拦截器
request.interceptors.response.use((resp) => {
    if (resp && resp.data.code === 401 && resp.data.message === '用户未登录！') {
        const currentUrl = window.location.href; // 记录当前访问页面
        window.location.href = '/user/login?redirect=' + currentUrl;
    }
    return resp;
})

// 4. 导出
export default request;